import * as api from '@/api/enrollment';

const state = {
  myEnrollments: [],
  currentEnrollment: null,
  materials: []
};

const mutations = {
  SET_MY_ENROLLMENTS(state, enrollments) {
    state.myEnrollments = enrollments;
  },
  SET_CURRENT_ENROLLMENT(state, enrollment) {
    state.currentEnrollment = enrollment;
  },
  SET_MATERIALS(state, materials) {
    state.materials = materials;
  }
};

const actions = {
  // 提交报名申请
  submitEnrollment({ commit }, enrollmentData) {
    return new Promise((resolve, reject) => {
      api.submitEnrollment(enrollmentData)
        .then(res => {
          commit('SET_CURRENT_ENROLLMENT', res.data);
          resolve(res);
        })
        .catch(err => {
          reject(err);
        });
    });
  },

  // 获取我的报名列表
  getMyEnrollments({ commit }) {
    return new Promise((resolve, reject) => {
      api.getMyEnrollments()
        .then(res => {
          commit('SET_MY_ENROLLMENTS', res.data);
          // 设置当前报名信息（如果有）
          if (res.data && res.data.length > 0) {
            commit('SET_CURRENT_ENROLLMENT', res.data[0]);
          }
          resolve(res.data);
        })
        .catch(err => {
          reject(err);
        });
    });
  },

  // 获取报名信息
  getEnrollment({ commit }, id) {
    return new Promise((resolve, reject) => {
      api.getEnrollment(id)
        .then(res => {
          commit('SET_CURRENT_ENROLLMENT', res.data);
          resolve(res.data);
        })
        .catch(err => {
          reject(err);
        });
    });
  },

  // 取消报名
  cancelEnrollment({ commit }, id) {
    return new Promise((resolve, reject) => {
      api.cancelEnrollment(id)
        .then(res => {
          // 更新当前报名信息
          commit('SET_CURRENT_ENROLLMENT', res.data);
          resolve(res);
        })
        .catch(err => {
          reject(err);
        });
    });
  },

  // 上传报名材料
  uploadMaterial({ commit }, { enrollmentId, materialType, filePath }) {
    return new Promise((resolve, reject) => {
      api.uploadMaterial(enrollmentId, materialType, filePath)
        .then(res => {
          // 更新材料列表
          return api.getMaterials(enrollmentId).then(materialsRes => {
            commit('SET_MATERIALS', materialsRes.data);
            resolve(res.data);
          });
        })
        .catch(err => {
          reject(err);
        });
    });
  },

  // 获取报名材料列表
  getMaterials({ commit }, enrollmentId) {
    return new Promise((resolve, reject) => {
      api.getMaterials(enrollmentId)
        .then(res => {
          commit('SET_MATERIALS', res.data);
          resolve(res.data);
        })
        .catch(err => {
          reject(err);
        });
    });
  },

  // 选择教练
  selectCoach({ commit }, { enrollmentId, coachId }) {
    return new Promise((resolve, reject) => {
      api.selectCoach(enrollmentId, coachId)
        .then(res => {
          // 更新当前报名信息
          commit('SET_CURRENT_ENROLLMENT', res.data);
          resolve(res);
        })
        .catch(err => {
          reject(err);
        });
    });
  },

  // 生成报名表
  generateEnrollmentForm({ commit }, enrollmentId) {
    return new Promise((resolve, reject) => {
      api.generateEnrollmentForm(enrollmentId)
        .then(res => {
          // 更新当前报名信息
          return api.getEnrollment(enrollmentId).then(enrollmentRes => {
            commit('SET_CURRENT_ENROLLMENT', enrollmentRes.data);
            resolve(res);
          });
        })
        .catch(err => {
          reject(err);
        });
    });
  },

  // 生成体检表
  generateMedicalForm({ commit }, enrollmentId) {
    return new Promise((resolve, reject) => {
      api.generateMedicalForm(enrollmentId)
        .then(res => {
          // 更新当前报名信息
          return api.getEnrollment(enrollmentId).then(enrollmentRes => {
            commit('SET_CURRENT_ENROLLMENT', enrollmentRes.data);
            resolve(res);
          });
        })
        .catch(err => {
          reject(err);
        });
    });
  }
};

const getters = {
  myEnrollments: state => state.myEnrollments,
  currentEnrollment: state => state.currentEnrollment,
  hasEnrollment: state => !!state.currentEnrollment,
  materials: state => state.materials,
  approvedEnrollments: state => state.myEnrollments.filter(item => item.status === 'approved')
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
};